package com.nanfang51g3.ckz.ui.forget;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.nanfang51g3.ckz.R;
import com.nanfang51g3.ckz.application.CkzApplication;
import com.nanfang51g3.ckz.ui.base.IPresenter;
import com.nanfang51g3.ckz.ui.base.Base2Activity;
import com.nanfang51g3.ckz.utils.AppManager;
import com.nanfang51g3.ckz.utils.ButtonUtils;
import com.nanfang51g3.ckz.utils.EasyToast;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.widget.ClearEditText;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/4/19.
 */
public class ForgetPassword2Activity extends Base2Activity implements ForgetPasswordContract.View {
    ForgetPasswordContract.ForgetPasswordActionsListener forgetPasswordActionsListener;
    @Bind(R.id.index_tv_right_title)
    TextView indexTvRightTitle;
    @Bind(R.id.bt_forget_password2_code)
    Button btForgetPassword2Code;
    @Bind(R.id.cet_registered2_code)
    ClearEditText cetRegistered2Code;
    String phone;
    private TimeCount time = new TimeCount(60 * 1000, 1000);
    @Override
    public IPresenter getPresenter() {
        return null;
    }
    @Override
    public int getContentView() {
        return R.layout.forget_password2;
    }

    @Override
    public boolean isShowLeftIcon() {
        return true;
    }

    @Override
    public boolean isShowRightIcon() {
        return false;
    }

    @Override
    public int setLeftIcon() {
        return R.mipmap.ic_accounting_left;
    }

    @Override
    public int setRightIcon() {
        return 0;
    }

    @Override
    public int showTitle() {
        return R.string.str_forget_password;
    }

    @Override
    public void onClickLeft(View v) {
        String value = btForgetPassword2Code.getText().toString().trim();
        String va = getResources().getString(R.string.str_get_code);
        if (value.equals(va)) {
            AppManager.getAppManager().finishActivity(this);
        }

    }

    @Override
    public void onClickRight(View v) {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void dismissProgress() {

    }

    @Override
    public void startShowView() {
        startActivity(new Intent(ForgetPassword2Activity.this, ForgetPassword3Activity.class).putExtra("phone",phone));
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showMessage(String message) {
        EasyToast.showShort(this,message);
    }

    @OnClick(R.id.bt_forget_password2_code)
    public void bt_forget_password2_code(View view) {
        btForgetPassword2Code.setEnabled(false);// 不可点击
        btForgetPassword2Code.setClickable(false);// 不可点击
        time.start();
        /**
         * @see  ForgetPasswordPresenter#getForgetPasswordCode(String, boolean)
         */
        forgetPasswordActionsListener.getForgetPasswordCode(phone,false);
    }

    /**
     * 倒计时
     *
     * @author Administrator
     */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {
            btForgetPassword2Code.setText(R.string.str_get_code);
            btForgetPassword2Code.setEnabled(true);// 可点击
            btForgetPassword2Code.setClickable(true);// 可点击
            btForgetPassword2Code.setPressed(false);// 未压下状态

        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            KLog.i("Evd",btForgetPassword2Code==null?"111-"+millisUntilFinished:"000-"+millisUntilFinished);
                btForgetPassword2Code.setText(String.format(getResStr(R.string.str_timer),
                        millisUntilFinished / 1000));
                btForgetPassword2Code.setPressed(true);// 压下状态

        }
    }

    public String getResStr(int id) {
        String str = "";
        try {
            str = getResources().getString(id);
        } catch (Resources.NotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            str = "";
        }
        return str;
    }
    @Override
    public void onIntent() {
        super.onIntent();
        phone = getIntent().getStringExtra("phone");
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        forgetPasswordActionsListener = DaggerForgetPasswordContractComponent
                .builder()
                .appComponent(CkzApplication.getAppComponent())
                .forgetPasswordPresenterModule(new ForgetPasswordPresenterModule(this))
                .build().getForgetPasswordPresenter();
        KLog.i("TAG", forgetPasswordActionsListener == null ? "forgetPasswordActionsListener==null" : "forgetPasswordActionsListener!=null");
        ButterKnife.bind(this);
        btForgetPassword2Code.setEnabled(false);// 不可点击
        btForgetPassword2Code.setClickable(false);// 不可点击
        time.start();
        indexTvRightTitle.setVisibility(View.VISIBLE);
        indexTvRightTitle.setText(getResources().getString(R.string.str_next));
        indexTvRightTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ButtonUtils.isFastDoubleClick(R.id.index_tv_right_title)) {
                    //写你相关操作即可
                    String code = cetRegistered2Code.getText().toString().trim();
                    /**
                     * @see ForgetPasswordPresenter#verificationCode(String, String)
                     */
                    forgetPasswordActionsListener.verificationCode(code, phone);
                }

            }
        });

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        time.cancel();
        ButterKnife.unbind(this);//解除绑定，官方文档只对fragment做了解绑
    }
}
